package cn.dglydrpy.study.mybatis.consumer.mapper;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.caches.redis.RedisCache;

import cn.dglydrpy.study.mybatis.consumer.entity.User;

//@CacheNamespace(implementation = RedisCache.class)//开启二级缓存
@CacheNamespace //开启mybatis默认的二级缓存
public interface UserMapper {
	
	public List<User> findAll();

	// 添加用户
	@Insert("insert into user values(#{id},#{username})")
	public void addUser(User user);

	// 更新用户
	@Update("update user set username = #{username} where id = #{id}")
	public void updateUser(User user);

	// 查询用户
	@Select("select * from user")
	public List<User> selectUser();

	// 删除用户
	@Delete("delete from user where id = #{id}")
	public void deleteUser(Integer id);

	// 根据id查询用户
	@Options(useCache = true) // 对单一sql确定是否开启二级缓存
	@Select({ "select * from user where id = #{id}" })
	public User findUserById(Integer id);

    //查询所有用户、同时查询每个用户关联的订单信息
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "orderList",column = "id",javaType = List.class,
                many=@Many(select = "cn.dglydrpy.study.mybatis.consumer.mapper.OrderMapper.findOrderByUid"))
    })
    public List<User> findAllUserAndOrder();

    //查询所有用户、同时查询每个用户关联的角色信息
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "roleList",column = "id",javaType = List.class,
             many = @Many(select = "cn.dglydrpy.study.mybatis.consumer.mapper.RoleMapper.findRoleByUid"))
    })
    public List<User> findAllUserAndRole();

}
